function createProtocolParser(options) {
    var parser = {
        weight: [],
        isAscii: options.isAscii != null ? options.isAscii : false,
        finish: false,
        start: options.start != null ? options.start : 2,
    };
    parser.add = function (data) {
        if ((parser.isAscii && data < 0) || parser.finish) return;
        if (data == parser.start) {
            //第二帧数据首帧和当前采集数据起始帧一样代表数据结束
            if (parser.weight[0] == parser.start) {
                parser.finish = true;
            } else {
                parser.weight = [data];
            }
        } else {
            if (parser.weight.length > 30) {
                parser.weight = [data];
            } else {
                parser.weight.push(data);
            }
        }
    }
    parser.isComplete = function () {
        return parser.finish;
    }
    parser.doParseWeight = function (weight) {

    }
    parser.parseWeight = function () {
        var filtered = [];
        for (var index in parser.weight) {
            var code = parser.weight[index];
            if (code != parser.start) {
                filtered.push(code);
            }
        }
        var weight = Number(parser.doParseWeight(filtered));
        parser.weight = [];
        parser.finish = false;
        return weight;
    }

    return parser
}

function createDS_1705Protocol() {
    var parser = createProtocolParser({isAscii: true})
    parser.doParseWeight = function (weight) {
        var pointPosition = Number.parseInt(String.fromCharCode(weight[10]));
        var codes = [];
        for (var index in weight) {
            codes.push(String.fromCharCode(weight[index]));
        }
        var number = codes.slice(2, 9);
        number.splice(number.length - pointPosition, 0, ".");
        return number.join("");
    }
    parser.name = "顶松1705通讯方式(6)";
    return parser
}

function createHBMWeightProtocol() {
    var parser = createProtocolParser({isAscii: true})
    parser.doParseWeight = function (weight) {
        return String.fromCharCode.apply(null, weight.slice(0, 7))
            .trim("")

    }
    parser.name = "HBM通用协议";
    return parser
}

function createHTWeightProtocol() {
    var parser = createProtocolParser({isAscii: false, start: -1})
    parser.doParseWeight = function (weight) {
        var symbol = weight[0].toString(16);
        return (
            (symbol == "31" || symbol == "21" ? "-" : "+") +
            weight[weight.length - 1].toString(16) +
            weight[weight.length - 1 - 1].toString(16) +
            weight[weight.length - 1 - 1 - 1].toString(16)
        );
    }
    parser.name = "恒天通用协议";
    return parser
}

function createTLDWeightProtocol() {
    var parser = createProtocolParser({isAscii: true})

    function parseDigitsPosition(byte) {
        /// 转为二进制数据,从右向左读取3位
        var binary = byte.toString(2);
        var data = [-1, -1, -1];
        if (binary.length >= 3) {
            data[0] = Number.parseInt(binary.slice(-1, binary.length)) + "";
            data[1] = Number.parseInt(binary.slice(-2, -1)) + "";
            data[2] = Number.parseInt(binary.slice(-3, -2)) + "";
        } else if (binary.length == 2) {
            data[0] = Number.parseInt(binary.slice(-1, binary.length)) + "";
            data[1] = Number.parseInt(binary.slice(-2, -1)) + "";
            data[2] = 0;
        } else if (binary.length == 1) {
            data[0] = Number.parseInt(binary.slice(-1, binary.length)) + "";
            data[1] = 0;
            data[2] = 0;
        }

        const digits = data.join("");
        if (digits == "110") {
            return 1;
        } else if (digits == "001") {
            return 2;
        } else if (digits == "101") {
            return 3;
        } else if (digits == "011") {
            return 4;
        } else if (digits == "111") {
            return 5;
        }
        return 0;
    }

    function parseNegative(byte) {
        var binary = byte.toString(2);
        if (binary.length >= 2) {
            var negative = binary.slice(-2, -1);
            if (negative == "1") {
                return "-";
            }
        }
        return "";
    }

    parser.doParseWeight = function (weight) {
        var digitsPosition = parseDigitsPosition(weight[0]);
        var negative = parseNegative(weight[1]);
        var number = [];
        var slicedWeight = weight.slice(3, 9);
        for (var index in slicedWeight) {
            number.push(String.fromCharCode(slicedWeight[index]));
        }
        number.unshift(negative);
        number.splice(number.length - digitsPosition, 0, ".");
        return number.join("");
    }
    parser.name = "托利多协议";
    return parser
}

function createXK3102K() {
    var parser = createProtocolParser({isAscii: true, start: 10})
    parser.doParseWeight = function (weight) {
        return String.fromCharCode.apply(null, weight.slice(6, 14))
            .split(" ")
            .join("");
    }
    parser.name = "金钟XK3102K";
    return parser
}

function createYaoWeightProtocol() {
    var parser = createProtocolParser({isAscii: true})
    parser.doParseWeight = function (weight) {
        var trimWeight = [];
        for (var index in weight) {
            var code = weight[index];
            if (code != 2 && code != 3 && code != 255) {
                trimWeight.push(code);
            }
        }
        var pointPosition = Number.parseInt(String.fromCharCode(trimWeight[7]));
        var number = [];
        var trimSliceWeight = trimWeight.slice(0, 7);
        for (var index in trimSliceWeight) {
            number.push(String.fromCharCode(trimSliceWeight[index]));
        }
        number.splice(number.length - pointPosition, 0, ".");
        return number.join("");
    }
    parser.name = "耀华通用协议";
    return parser
}

function createYaoXk3190A9() {
    var parser = createProtocolParser({isAscii: true, start: 61})
    parser.doParseWeight = function (weight) {
        weight.reverse();
        return String.fromCharCode.apply(null, weight);
    }
    parser.name = "耀华XK3190-A9";
    return parser
}

function getProtocolCreator(){
    return {
        DS_1705Protocol: createDS_1705Protocol,
        HBMWeightProtocol: createHBMWeightProtocol,
        HTWeightProtocol: createHTWeightProtocol,
        TLDWeightProtocol: createTLDWeightProtocol,
        XK3102K: createXK3102K,
        YaoWeightProtocol: createYaoWeightProtocol,
        YaoXk3190A9: createYaoXk3190A9,
    }
}